﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MetroDataApi.Models
{
    public class QueryResults
    {
        public static string GetOriginTime(int prjId, string ptName)
        {
            using (var db = new MetroDataDataContext())
            {
                var oTime = (from p in db.MonitorPoints
                             where p.Name == ptName && p.PrjId == prjId
                             orderby p.DateTime ascending
                             select p.DateTime).FirstOrDefault();
                return oTime.Value.Date.ToString("yyyy-MM-dd");
            }
        }

        public static List<result> GetOriginResults(int prjId, List<string> names)
        {
            try
            {
                using (var db = new MetroDataDataContext())
                {
                    var list = (from p in db.MonitorPointsDictionary
                                where p.PrjId == prjId && names.Contains(p.Name)
                                select p).ToList();

                    var values = list.Where(p => p != null).Select(v => new result()
                    {
                        name = v.Name,
                        time = v.OriginDate.Value.ToString("yyyy-MM-dd"),
                        value = v.OriginValue.HasValue ? v.OriginValue.Value : double.NaN
                    }).ToList();
                    return values;
                }
            }
            catch
            {
                return new List<result>();
            }
        }

        public static IEnumerable<result> GetReviewResultsByTime(int prjId, string timeStr, List<string> names)
        {
            using (var db = new MetroDataDataContext())
            {
                var date = DateTime.Parse(timeStr);


                var list = (from n in names
                            join p in db.PreReviewMonitorPoints.Where(p => prjId == p.PrjId && p.DateTime.Value.Date == date)
                            on n equals p.Name into gs
                            select new result()
                            {
                                name = n,
                                time = timeStr,
                                value = gs.FirstOrDefault() != null ? gs.FirstOrDefault().Value.Value : double.NaN,
                            }).ToList();
                return list;
            }
        }


        public static IEnumerable<result> GetResultsByTime(int prjId, string timeStr, List<string> names)
        {

            try
            {
                using (var db = new MetroDataDataContext())
                {
                    var time = DateTime.Parse(timeStr);

                    var needHMS = time.Hour == 0 && time.Minute == 0 && time.Second == 0 ? false : true;
                    var tStr = needHMS ? time.ToString("yyyy-MM-dd HH:mm:ss") : time.ToString("yyyy-MM-dd");
                    var listPt = new List<MonitorPoints>();
                    if (needHMS)
                    {
                        listPt = (from p in db.MonitorPoints
                                  where p.PrjId == prjId && p.DateTime.Value == time
                                  select p).ToList();
                    }
                    else
                    {
                        listPt = (from p in db.MonitorPoints
                                  where p.PrjId == prjId && p.DateTime.Value.Date == time.Date
                                  select p).ToList();
                    }

                    List<MonitorPoints> vv = new List<MonitorPoints>();


                    var list = (from n in names
                                join p in listPt on n equals p.Name into gs
                                from gi in gs.DefaultIfEmpty()
                                select new result()
                                {
                                    name = n,
                                    time = tStr,
                                    value = gi != null ? gs.FirstOrDefault().Value.Value : double.NaN,
                                    //count = gs.Count()
                                }).ToList();

                    return list;
                }
            }
            catch
            {
                return new List<result>();
            }
        }

        //根据给出的2次结果数据:初始值和当前值,计算这一期的变化量.
        public static IEnumerable<result> GetDiffBy2ResultsList(IEnumerable<result> origin, IEnumerable<result> values, bool isDesign = false)
        {
            if (!isDesign)
            {
                var results = (from o in origin
                               join p in values
                               on o.name equals p.name
                               into gr
                               from g in gr.DefaultIfEmpty()
                               select new result()
                               {
                                   name = o.name,
                                   time = g.time,
                                   value = g != null && !double.IsNaN(o.value) ? Math.Round((g.value - o.value) * 1000, 2) : double.NaN//取毫米
                               }).ToList();
                return results;
            }
            else
            {
                return values.Select(p =>
                            new result() { name = p.name, time = p.time, value = Math.Round((p.value - 5.5) * 1000, 2) }).ToList();
            }

        }

    }
}